package com.ssyc.gsk_teacher.activity;

import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;

import com.ssyc.common.adapter.VpCommonAdapter;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.view.NoScrollViewPager;
import com.ssyc.gsk_teacher.R;
import com.ssyc.gsk_teacher.fragment.TeacherFriendsFragment;
import com.ssyc.gsk_teacher.fragment.TeacherGroupFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 家长端--通讯录界面
 * Created by Administrator on 2018/5/15 0015.
 */

public class TeacherAddressBookActivity extends BaseActivity implements View.OnClickListener {

    //返回键
    private ImageView ivBack;

    //好友
    private ImageView lvFriends;

    //群组
    private ImageView lvGroup;

    //ViewPager
    private NoScrollViewPager vp;

    //Tab页面选中的资源图片集合
    private int[] tabSelectIds = {R.drawable.common_friend_select, R.drawable.common_group_select};

    //Tab页面未选中的资源图片集合
    private int[] tabUnselectIds = {R.drawable.common_friend_normal, R.drawable.common_group_normal};


    //填充VP的Fragments集合
    private List<Fragment> mFragments;
    //ViewPager的适配器
    private VpCommonAdapter vpAdapter;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.teacher_activity_address_book;
    }

    @Override
    protected void init() {

        showContent();

        //初始化视图
        initView();

        //设置ViewPager
        initVp();
    }

    /**
     * 初始化视图的方法
     */
    public void initView() {
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        lvFriends = findViewById(R.id.lv_friends);
        lvFriends.setOnClickListener(this);
        lvGroup = findViewById(R.id.lv_group);
        lvGroup.setOnClickListener(this);
        vp = findViewById(R.id.vp);
    }

    /**
     * 初始化VieWPager
     */
    private void initVp() {
        mFragments = new ArrayList<>();
        mFragments.add(TeacherFriendsFragment.newInstance());
        mFragments.add(TeacherGroupFragment.newInstance());
        vpAdapter = new VpCommonAdapter(getSupportFragmentManager(), mFragments);
        vp.setOffscreenPageLimit(mFragments.size());
        vp.setPagerEnabled(false);
        vp.setAdapter(vpAdapter);
    }

    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.lv_friends) {
            //好友
            syncVpState(0);
        } else if (id == R.id.lv_group) {
            //群组
            syncVpState(1);
        }
    }

    /**
     * 同步ViewPager页面和切换选中Tab的方法
     *
     * @param pos
     */
    private void syncVpState(int pos) {
        vp.setCurrentItem(pos);
        if (pos == 0) {
            lvFriends.setImageResource(tabSelectIds[0]);
            lvGroup.setImageResource(tabUnselectIds[1]);
        } else if (pos == 1) {
            lvFriends.setImageResource(tabUnselectIds[0]);
            lvGroup.setImageResource(tabSelectIds[1]);
        }
    }
}
